#!/bin/bash

# 等待数据库可用
while ! nc -z postgres 5432; do
  echo "等待 PostgreSQL 启动..."
  sleep 1
done

# 初始化 Airflow 数据库 (只在第一次运行时执行)
if [ "$1" = "webserver" ]; then
    airflow db check || airflow db init
    
    # 创建管理员用户
    airflow users create \
        --username admin \
        --firstname Admin \
        --lastname User \
        --role Admin \
        --email admin@dataworks.com \
        --password admin123 || true
fi

# 执行原始命令
exec airflow "$@" 